﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Runtime.Caching;
using System.Net;
using System.IO;
using System.Data.SqlClient;
using Aweber;
using Aweber.Entity;

namespace Aweber.Example
{
    public partial class Authorize : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            // Replace with the keys of your application
            // NEVER SHARE OR DISTRIBUTE YOUR APPLICATIONS'S KEYS!
            // It is recommended you place these within your web.config application settings
            String consumerKey = ConfigurationManager.AppSettings["AweberConsumerKey"]; // "vjckuVc3sNrZWOa3PWnf";
            String consumerSecret = ConfigurationManager.AppSettings["AweberConsumerSecret"]; // "u3sQ7vGGJZPCKhgEOAXeqgBSi685c5x2wm6gZuIj";

            // Create a new api instance
            API api = new API(consumerKey, consumerSecret);

            api.OAuthToken = "Previously Stored Token";

            // Fill the tokens back from user session
            api.OAuthToken = Convert.ToString(Session["oauth_token"]);
            api.OAuthTokenSecret = Convert.ToString(Session["oauth_token_secret"]);

            // Get the returned oauth_verifier
            api.OAuthVerifier = Request["oauth_verifier"];

            // Get Access Token (this is the permanent token to be stored for future access)
            api.get_access_token();
            
            // Get your account
            Account account = api.getAccount();

            // Loop through all lists in your account
            foreach (Entity.List list in account.lists().entries)
            {
                // Output list values to a table
                Table t = new Table();

                Literal l = new Literal();
                l.Text = String.Format("<h1>List: {0}</h1>", list.name);
                form1.Controls.Add(l);

                Literal l2 = new Literal();
                l2.Text = String.Format("<h3>{0}</h3>", list.id);
                form1.Controls.Add(l2);

                TableRow row = new TableRow();

                row.Cells.Add(new TableCell() { Text = "Subject" });
                row.Cells.Add(new TableCell() { Text = "Sent" });
                row.Cells.Add(new TableCell() { Text = "Stats" });

                t.Rows.Add(row);


                // Loop through all campaigns in your list
                foreach (Campaign campaign in list.campaigns().entries)
                {

                    if (campaign.type == "broadcast_campaign")
                    {
                        TableRow campaignRow = new TableRow();
                        campaignRow.Cells.Add(new TableCell() { Text = String.Format("<em>{0}</em>", campaign.subject) });
                        campaignRow.Cells.Add(new TableCell() { Text = campaign.sent_at.Value.ToString("MMM dd, YYYY hh:mm t") });
                        campaignRow.Cells.Add(new TableCell() { Text = String.Format("<ul><li><b>Opened:</b> {0}</li><li><b>Sent:</b> {1}</li><li><b>Clicked:</b> {2}</li></ul>", campaign.total_opens, campaign.total_sent, campaign.total_clicks) });

                        t.Rows.Add(campaignRow);
                    }

                }

                // Add table to page output
                form1.Controls.Add(t);
            }

        }
    }
}